/**
 * 量算工具
 */
define([
    'require',
    './init'
], function(require, init) {
    'use strict';
    var viewer = init.viewer;
    var position = init.position;

    /**
     * @class
     * @module
     */
    function MeasureTool(){
        this._handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        this._polylinePath = new Array();   // 折线路径
        this._distance = 0;
        this._polygonPath = new Array();    // 多边形路径
        this._area = 0;
    }
    /**
     * 绘制折线
     */
    MeasureTool.prototype.drawPolyline = function(isPaste){

        // 绘制点
        var len = this._polylinePath.length,
            point1 = this._polylinePath[len-1];

        if(len>=2){
            // 计算距离
            this.calcDistance(isPaste);
            var point2 = this._polylinePath[len-2];
            // 绘制线
            viewer.entities.add({
                polyline : {
                    positions : Cesium.Cartesian3.fromDegreesArray([
                        point1.x, point1.y,
                        point2.x, point2.y
                    ]),
                    width : 2,
                    material : Cesium.Color.RED,
                    height : 0,
                    heightReference : Cesium.HeightReference.RELATIVE_TO_GROUND
                }
            });
        }

        var text = len == 1 ? '起点' : this._distance.toFixed(2)+'米';
        viewer.entities.add({
            position : Cesium.Cartesian3.fromDegrees(point1.x, point1.y),
            point : {
                pixelSize : 10,
                color : Cesium.Color.RED,
                outlineColor : Cesium.Color.YELLOW.withAlpha(0.3),
                outlineWidth : 1
            },
            label : {
                text : text,
                show : true,
                showBackground : true,
                font : '12px monospace',
                verticalOrigin : Cesium.VerticalOrigin.TOP,
                horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
                pixelOffset : new Cesium.Cartesian2(15, 0)
            }
        })
        
        
    }
    /**
     * 绘制多边形
     */
    MeasureTool.prototype.drawPolygon = function(){

    }
    /**
     * 测量距离
     * @param {} isPaste 是否贴地 
     */
    MeasureTool.prototype.measureDistance = function(isPaste){
        var handler = this._handler;
        var that = this;
        // 鼠标单击事件
        handler.setInputAction(function(){
            // 添加点
            var point = Cesium.clone(position.mousePositionOnWSG);
            that._polylinePath.push(point);

            // 绘制点
            that.drawPolyline(isPaste);

        },Cesium.ScreenSpaceEventType.LEFT_CLICK);
        // 鼠标双击事件
        handler.setInputAction(function(){
            handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
            handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        },Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    }
    /**
     * 测量面积
     * @param {} isPaste 是否贴地 
     */
    MeasureTool.prototype.measureArea = function(isPaste){

    }
    /**
     * 计算距离
     */
    MeasureTool.prototype.calcDistance = function(isPaste){
        // 取出最后两个点
        var len = this._polylinePath.length,
            point1 = this._polylinePath[len-1],
            point2 = this._polylinePath[len-2];

        // 计算距离
        var distance = 0;
        if(isPaste){
            var geodesic = new Cesium.EllipsoidGeodesic();
            geodesic.setEndPoints(
                Cesium.Cartographic.fromDegrees(point1.x, point1.y, point1.z),
                Cesium.Cartographic.fromDegrees(point2.x, point2.y, point2.z)
            );
            distance=geodesic.surfaceDistance;
        }else{
            distance = Cesium.Cartesian3.distance(
                Cesium.Cartesian3.fromDegrees(point1.x, point1.y, point1.z),
                Cesium.Cartesian3.fromDegrees(point2.x, point2.y, point2.z),
            );
        }
        this._distance += distance;
    }
    return MeasureTool;
});